<?php

require_once 'Zend/View.php';
require_once 'Zend/Controller/Front.php';

class Core_Widget_Abstract
{
	protected $_view;
	protected $_path;
	
	public function __construct($path)
	{
		$instance = Core_Widget::getInstance();
		$template = Core_Templates::getInstance();
		
    $this->_view = new Zend_View($instance->getConfigs()->resources->view->toArray());
    $this->_view->addBasePath($instance->getPath($path))
								->addBasePath($template->getPath('_/'.$path));
		$this->_path = $path;

		$this->init();
	}
	
	protected function init()
	{
    
	}
	
	public function __call($method, array $options)
	{
		if (method_exists(Core_Controller_Action::getInstance(), $method)) {
				return call_user_func_array(array(
					Core_Controller_Action::getInstance(), $method), $options);
		}
		die("Unknown method '" . $method . "' called!");
	}

	public function setOption($key, $val = null)
	{
    $options = is_array($key)?$key:array($key=>$val);
    
    foreach($options as $key=>$val)
      $this->getView()->{$key} = $val;
		return $this;
	}
  
	/**
	* getView
	*
	* @return Zend_View object
	*/
	public function getView()
	{
		return $this->_view;
	}
  
  /**
	* render
	*
	* @return string
	*/
	public function render($view = 'render')
	{
		return $this->getView()->render($view.'.phtml');
		/*
		$a = microtime();
		$b = memory_get_usage();
		return $this->getView()->render($view.'.phtml').'
			<!-- '.$this->_path.'
			::'.number_format((microtime()-$a),2).' s/ '.number_format(((memory_get_usage()-$b)/(1024*1024)),2).' MB::-->';
		*/
	}
}